package com.maistars.spider.service.render.impl;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.maistars.spider.common.exception.TopicSpiderException;
import com.maistars.spider.service.render.PageRender;
import com.maistars.spider.service.domain.render.PageRequest;
import com.maistars.spider.service.domain.render.PageResponse;
import com.maistars.spider.service.render.handler.HtmlHandler;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.logging.Level;

/**
 * @author dhr
 * @date 2021/11/13 下午9:19
 */
public class SimpleRender extends AbstractPageRender {
    @Override
    public PageResponse renderPage(PageRequest pageRequest) throws TopicSpiderException {
        PageResponse pageResponse = new PageResponse();
        pageResponse.setStatus(1);
        pageResponse.setUrl(pageRequest.getUrl());

        if (pageRequest.getRenderConfig().getDynamic()) {
            htmlUnitRender(pageRequest, pageResponse);
        } else {
            jsoupRender(pageRequest, pageResponse);
        }

        return pageResponse;
    }

    private PageResponse jsoupRender(PageRequest pageRequest, PageResponse pageResponse) {
        Document document = null;
        try {
            document = Jsoup.connect(pageRequest.getUrl()).timeout(pageRequest.getRenderConfig().getPageLoadTimeout()*1000).get();
            String html = document.html();
            handleHtml(pageRequest, pageResponse, html);
            pageResponse.setStatus(0);
            pageResponse.setRenderResult(html);
        } catch (Exception e) {
            pageResponse.setRenderResult("can't load this page");
        }
        return pageResponse;
    }

    private PageResponse htmlUnitRender(PageRequest pageRequest, PageResponse pageResponse) throws TopicSpiderException {
        try {
            //构造一个webClient 模拟Chrome 浏览器
            WebClient webClient = new WebClient(BrowserVersion.CHROME);
            java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
            //支持JavaScript
            webClient.getOptions().setJavaScriptEnabled(true);
            webClient.getOptions().setCssEnabled(true);
            webClient.getOptions().setActiveXNative(false);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setTimeout(pageRequest.getRenderConfig().getPageLoadTimeout() * 1000);
            webClient.setAjaxController(new NicelyResynchronizingAjaxController());
            webClient.getOptions().setDoNotTrackEnabled(false);

//            webClient.getCurrentWindow().setInnerHeight(Integer.MAX_VALUE);

            HtmlPage rootPage = webClient.getPage(pageRequest.getUrl());
            //设置一个运行JavaScript的时间
            webClient.waitForBackgroundJavaScript(pageRequest.getRenderConfig().getScriptLoadTimeout() * 1000);
            String html = rootPage.asXml();
            webClient.close();
            handleHtml(pageRequest, pageResponse, html);
            pageResponse.setStatus(0);
        } catch (IOException ioException) {
            pageResponse.setRenderResult("can't load this page");
        }
        return pageResponse;
    }


}
